#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5,M=1e6+5;
int T,n,a[N],mat[N],mp[M];
long long dp[N],pre[N];
void solve(){
    cin>>n;
    memset(mp,0,sizeof mp);
    for(int i=1;i<=n;i++){
        cin>>a[i];
        mat[i]=mp[a[i]];
        mp[a[i]]=i;
    }
    pre[1]=0;
    for(int i=2;i<=n;i++)
        pre[i]=pre[i-1]+(mat[i]==i-1)*a[i];
    memset(dp,0,sizeof dp);
    for(int i=1;i<=n;i++){
        if(!mat[i]){
            dp[i]=dp[i-1];
            continue;
        }
        long long sum=a[i];
        if(mat[i]==i-1) sum+=dp[mat[i]];
        else sum+=dp[mat[i]+1];
        sum+=max(0ll,pre[i-1]-pre[mat[i]+1]);
        dp[i]=max(dp[i-1],sum);
    }
    cout<<dp[n]<<'\n';
}
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>T;
    while(T--) solve();
    return 0;
}
